// 还是需呀一个加载中的判定
//  let ip = 'http://123.60.21.194:862';
// let ip = 'https://ics.cnncaccuray.com';
let ip = 'http://192.168.8.220:8044' // 测试外网
// let ip = 'http://192.168.28.93:8040';//wangyh
// let ip = 'http://192.168.28.192:8040';//liu
let proxy = ip + '/api/'
// get请求
function getObject(resourceName, id, isAnonymous) {
    let url = proxy + resourceName + '/' + id;
    return myAxios(url, null, 'GET');
}

// query get请求
function query(resourceName, page, condition, orderBy, isAnonymous) {
    let url = proxy + resourceName + '?';
    if (condition) {
        url += 'condition=' + encodeURIComponent(JSON.stringify(condition)) + '&'
    }
    let pageIndex = page && Object.keys(page).length ? page.pageIndex : -1
    let pageSize = page && Object.keys(page).length ? page.pageSize : -1
    url += 'pageIndex=' + pageIndex
    url += '&pageSize=' + pageSize
    if (orderBy) url += '&orderBy=' + orderBy
    return myAxios(url, null, 'GET',);
}
// POST请求

// 自定义请求
function custom(resourceName, methodName, model, isAnonymous) {
    let url = proxy + resourceName + '/' + methodName;
    return myAxios(url, model, 'POST');
}

// addAPI
function add(resourceName, model, isAnonymous) {
    let url = proxy + resourceName
    return myAxios(url, model, 'POST')
}
// 删除API

function remove(resourceName, id, isAnonymous) {
    let url = proxy + resourceName
    if (id && id != '') {
        url += '/' + id
    }
    return myAxios(url, {}, 'DELETE')
}

// PUT请求
// 更新
function update(resourceName, model, isAnonymous) {
    let url = proxy + resourceName
    return myAxios(url, model, 'PUT');
}
// 批量操作资源
function saveObjects(resourceName, insertObjects, updateObjects, deleteObjects, isAnonymous) {
    let url = proxy + resourceName + '/batch';
    let data = {
        insertObjects: insertObjects,
        updateObjects: updateObjects,
        deleteObjects: deleteObjects,
    }
    return myAxios(url, data, 'POST')
}
// 更新
function addOrUpdate(resourceName, model, isUpdate, isAnonymous) {
    if (isUpdate && isUpdate == true) {
        return update(resourceName, model, isAnonymous)
    } else {
        return add(resourceName, model, isAnonymous)
    }
}
// 请求后端接口 原生axios
function jrsfRequest(resourceName, data, method) {
    let url = ip + resourceName
    return myAxios(url, data, method);
}

function noToken(api, data, method) {
    let url = proxy + api;
    console.log(url, '地址')
    let promise = new Promise((resolve, reject) => {
        wx.showLoading({
            title: '加载中'
        })
        wx.request({
            url: url,
            data: data,
            header: {
                'content-type': 'application/json',
                "Authorization": '',
                "type": "wx"
            },
            method: method || 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {

                let res = result.data
                if (res.code == '501') {
                    wx.showModal({
                        content: res.msg,
                        showCancel: false,
                        success(res) {
                            if (res.confirm) {
                                resolve(res)
                            } else if (res.cancel) {

                            }
                        }
                    })
                } else {
                    resolve(res)
                }



            },
            fail: (e = {
                code: -1,
                msg: errMsg,
                errMsg
            }) => {
                let msg = e.errMsg;
                if (e.errMsg == 'request:fail time out') {
                    msg = '服务请求超时,请稍后重试'
                }
                wx.showToast({
                    title: msg,
                    icon: 'none'
                });
                reject(e);
                console.log('报错的话')
            },
            complete: () => {
                wx.hideLoading()
            }
        });
    })
    return promise
}

function addFix(resourceName, model, isAnonymous) {
    let url = proxy + resourceName
    return myAxiosFix(url, model, 'POST')
}
let myAxiosFix = function (url, data, method) {
    let token = wx.getStorageSync('token');
    let promise = new Promise((resolve, reject) => {
        wx.showLoading({
            title: '加载中'
        })
        wx.request({
            url: url,
            data: data,
            header: {
                'content-type': 'application/json',
                "Authorization": token,
                "type": "wx"
            },
            method: method || 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {

                let res = result.data
                if (res.code == 401) {
                    // 做防抖处理
                    // 先取出来保存
                    let username = wx.getStorageSync('cacheName')
                    wx.clearStorage() // 置空token
                    // 再写入
                    wx.setStorageSync('cacheName', username);

                    // 重定向到绑卡页面
                    wx.showToast({
                        title: '请先登录',
                        icon: "error",
                        success() {
                            wx.reLaunch({
                                url: '/packageA/login/login',
                                success: (result) => {
                                },
                                fail: () => { },
                                complete: () => { }
                            });

                        }
                    })
                } else {
                    if (res.code == '501') {
                        wx.showModal({
                            content: res.msg,
                            showCancel: false,
                            success(temp) {
                                if (temp.confirm) {
                                    res.click = true
                                } else if (temp.cancel) {
                                    res.click = false
                                }
                                resolve(res)
                            }
                        })
                    } else {
                        resolve(res)
                    }

                }


            },
            fail: (e = {
                code: -1,
                msg: errMsg,
                errMsg
            }) => {
                let msg = e.errMsg;
                if (e.errMsg == 'request:fail time out') {
                    msg = '服务请求超时,请稍后重试'
                }
                wx.showToast({
                    title: msg,
                    icon: 'none'
                });
                reject(e);
                console.log('报错的话')
            },
            complete: () => {
                wx.hideLoading()
            }
        });
    })
    return promise
}
let myAxios = function (url, data, method) {
    let token = wx.getStorageSync('token');
    let promise = new Promise((resolve, reject) => {
        wx.showLoading({
            title: '加载中'
        })
        wx.request({
            url: url,
            data: data,
            header: {
                'content-type': 'application/json',
                "Authorization": token,
                "type": "wx"
            },
            method: method || 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {

                let res = result.data
                if (res.code == 401) {
                    // 做防抖处理
                    // 先取出来保存
                    let username = wx.getStorageSync('cacheName')
                    wx.clearStorage() // 置空token
                    // 再写入
                    wx.setStorageSync('cacheName', username);

                    // 重定向到绑卡页面
                    wx.showToast({
                        title: '请先登录',
                        icon: "error",
                        success() {
                            wx.reLaunch({
                                url: '/packageA/login/login',
                                success: (result) => {
                                },
                                fail: () => { },
                                complete: () => { }
                            });

                        }
                    })
                } else {
                    if (res.code == '501') {
                        wx.showModal({
                            content: res.msg,
                            showCancel: false,
                        })
                    } else {
                        resolve(res)
                    }

                }


            },
            fail: (e = {
                code: -1,
                msg: errMsg,
                errMsg
            }) => {
                let msg = e.errMsg;
                if (e.errMsg == 'request:fail time out') {
                    msg = '服务请求超时,请稍后重试'
                }
                wx.showToast({
                    title: msg,
                    icon: 'none'
                });
                reject(e);
                console.log('报错的话')
            },
            complete: () => {
                wx.hideLoading()
            }
        });
    })
    return promise
}
module.exports = {
    getObject,
    query,
    add,
    remove,
    update,
    saveObjects,
    custom,
    addOrUpdate,
    jrsfRequest,
    addFix,
    ip,
    noToken,
}